<?php
/**
 * 用户错误处理类
 * $Id$
 */

class error {
	/**
	 * 错误消息
	 */
	private $message = array();

	/**
	 * 错误显示模版
	 */
	private $template = '';

	/**
	 * 错误号码
	 */
	private $error_no = 0;

	/**
	 * 构造函数
	 */
	public function __construct($tpl) {
		$this->template = $tpl;
	}

	/**
	 * 添加一条错误信息
	 */
	public function add($msg, $errno = 1) {
		if (is_array($msg)) {
			$this->message = array_merge($this->message, $msg);
		} else {
			$this->message = $msg;
		}
		$this->error_no = $errno;
	}

	/**
	 * 清空作物信息
	 */
	public function clean() {
		$this->message = array();
		$this->errno = 0;
	}

	/**
	 * 返回所有错误信息
	 */
	public function get_all() {
		return $this->message;
	}

	/**
	 * 返回最后一条错误信息
	 */
	public function last_message() {
		return array_slice($this->message, -1);
	}

	/**
	 * 显示错误信息
	 */
	public function show($href = '') {
		if ($this->error_no > 0) {
			$message = array();
			$href = (empty($href)) ? 'javascript:history.back();' : $href;
			$message['back_url'] = $href;

			foreach ($this->message as $msg) {
				$message['content'] = '<div' . htmlspecialchars($msg) . '</div>';
			}

			if (isset($GLOBALS['smarty'])) {
				assign_template();
				$GLOBALS['smarty']->assign('auto_redirect', true);
				$GLOBALS['smarty']->assign('message', $message);
				$GLOBALS['smarty']->display($this->template);
			} else {
				die($message['content'];
			}
			exit;
		}
	}
}
